package com.example.moshudriver.model;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.BeeFramework.model.BaseModel;
import com.BeeFramework.model.BeeCallback;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.protocol.CONTENT;
import com.example.moshudriver.protocol.reportRequest;
import com.example.moshudriver.protocol.reportResponse;
import com.example.moshudriver.tools.O2OMobileAppConst;
import com.example.moshudriver.tools.SESSION;
import com.external.androidquery.callback.AjaxStatus;

public class ReportModel extends BaseModel {

	public ReportModel(Context context) {
		super(context);
	}

	public void report(int orderId, int userId, String text) {
		reportRequest request = new reportRequest();
		request.sid = SESSION.getInstance().sid;
		request.uid = SESSION.getInstance().uid;
		request.ver = O2OMobileAppConst.VERSION_CODE;
		request.order_id = orderId;
		request.user = userId;
		CONTENT content = new CONTENT();
		content.text = text;
		request.content = content;

		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				try {
					ReportModel.this.callback(this, url, jo, status);
					if (null != jo) {
						reportResponse response = new reportResponse();
						response.fromJson(jo);
						if (response.succeed == 1) {
							ReportModel.this.OnMessageResponse(url, jo, status);
						} else {
							ReportModel.this.callback(url, response.error_code,
									response.error_desc);
						}
					}
				} catch (JSONException e) {

				}
			}
		};

		Map<String, Object> params = new HashMap<String, Object>();
		try {
			params.put("json", request.toJson().toString());

			JSONObject jsonRequest = request.toJson();
			if (orderId == 0) {
				jsonRequest.remove("order_id");
			}
			if (userId == 0) {
				jsonRequest.remove("user");
			}
			params.put("json", jsonRequest.toString());
		} catch (JSONException e) {

		}
		cb.url(ApiInterface.REPORT).type(JSONObject.class).params(params);
		ajaxProgress(cb);
	}

	public void feedback(String text) {
		reportRequest request = new reportRequest();
		request.sid = SESSION.getInstance().sid;
		request.uid = SESSION.getInstance().uid;
		request.ver = O2OMobileAppConst.VERSION_CODE;
		CONTENT content = new CONTENT();
		content.text = text;
		request.content = content;

		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				try {
					ReportModel.this.callback(this, url, jo, status);
					if (null != jo) {
						reportResponse response = new reportResponse();
						response.fromJson(jo);
						if (response.succeed == 1) {
							ReportModel.this.OnMessageResponse(url, jo, status);
						} else {
							ReportModel.this.callback(url, response.error_code,
									response.error_desc);
						}
					}
				} catch (JSONException e) {

				}
			}
		};

		Map<String, Object> params = new HashMap<String, Object>();
		try {
			params.put("json", request.toJson().toString());

		} catch (JSONException e) {

		}
		cb.url(ApiInterface.FEEDBACK).type(JSONObject.class).params(params);
		ajaxProgress(cb);
	}
}
